import Vue from 'vue'
import VueRouter from 'vue-router'
import Base from '../views/Base.vue'
import User from '../views/User/UserList'
import Home from "@/views/home/HomeView";
import Cookies from 'js-cookie'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login')
  },
  {
    path: '/',
    name: 'Base',
    component: Base,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/home/HomeView')
      },
      {
        path: 'userList',
        name: 'UserList',
        component: User
      },
      {
        path: 'addUser',
        name: 'AddUser',
        component: () => import('@/views/User/AddUser')
      },
      {
        path: 'editUser',
        name: 'EditUser',
        component: () => import('@/views/User/EditUser')
      },
      {
        path: 'addAdmin',
        name: 'AddAdmin',
        component: () => import('@/views/Admin/AddAdmin')
      },
      {
        path: 'editAdmin',
        name: 'EditAdmin',
        component: () => import('@/views/Admin/EditAdmin')
      },
      {
        path: 'AdminList',
        name: 'AdminList',
        component: () => import('@/views/Admin/AdminList')
      },
      //  ====  Category  ====
      { path: 'categoryList', name: 'CategoryList', component: () => import('@/views/category/List.vue') },
      { path: 'addCategory', name: 'AddCategory', component: () => import('@/views/category/Add.vue') },
      { path: 'editCategory', name: 'EditCategory', component: () => import('@/views/category/Edit.vue') },
      //  ====  Book  ====
      { path: 'bookList', name: 'BookList', component: () => import('@/views/book/List.vue') },
      { path: 'addBook', name: 'AddBook', component: () => import('@/views/book/Add.vue') },
      { path: 'editBook', name: 'EditBook', component: () => import('@/views/book/Edit.vue') },
    ]
  },
    /*404路由*/
  {
    path: '*',
    component: () => import('@/views/404')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') next()
  const admin = Cookies.get("admin")
  if (!admin && to.path !== '/login') return next("/login")
  next()
})
export default router
